﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using ObjectLibrary;
namespace _0912089_0912172
{
    
    public class Bar : VisibleGameEntity2D
    {
        public enum BAR_TYPE {UP_DOWN,QUATER_CIRCLE};
        BAR_TYPE _type;
        private float _value;
        TextureUntity texureUntity;
        public float Value
        {
            get { return _value; }
            set {
                if (value <= 0)
                {
                    _value = 0;
                    return;
                }
                if (_type == BAR_TYPE.UP_DOWN)
                {
                    int w = _bar.Width;
                    int h = _bar.Height;
                    h = (int)(value * h);
                    if (h != 0)
                        _displayBar = texureUntity.splitTexture(_bar, new Rectangle(0, _bar.Height - h, w, h));
                }
                else if (_type == BAR_TYPE.QUATER_CIRCLE)
                {
                    if (value >= 1)
                        _displayBar = _bar;
                    else
                    {
                        double angle = value * MathHelper.PiOver2;

                        _displayBar = texureUntity.splitTexture(_bar, angle);
                    }
                }
                _value = value;
            }
        }


        private Texture2D _bar;
        private Texture2D _displayBar;
    

        public Bar(ContentManager content,GraphicsDevice graphics,string back, string front,BAR_TYPE type)
        {
            _texture = content.Load<Texture2D>(back);
            _bar = content.Load<Texture2D>(front);
            texureUntity = TextureUntity.Create(content, graphics);
            Value = 1;
            _type = type;

        }
        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            throw new NotImplementedException();
        }
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Rectangle screenRect)
        {

            
            if (_value > 0)
            {
                //Rectangle rec = new Rectangle(screenRect.Left, screenRect.Top, (int)(_value * screenRect.Width), screenRect.Height);
                spriteBatch.Draw(_displayBar, screenRect, new Rectangle(0, 0, _displayBar.Width, _displayBar.Height), Color.White);
            }
            spriteBatch.Draw(_texture, screenRect, new Rectangle(0, 0, _texture.Width, _texture.Height), Color.White);
        }
    }
}
